package com.wu.ssm.spring.aop;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 代理对象管理器
 * Created by wzf on 2018/11/25/025.
 */
public class ProxyManager {

//    @SuppressWarnings("unchecked")
//    public static <T> T creatProxy(final Class<?> targetClazz, final List<Proxy> proxyList){
//        return (T) Enhancer.create(targetClazz, new MethodInterceptor() {
//            @Override
//            public Object intercept(Object targetObject, Method targetMethod, Object[] params, MethodProxy methodProxy) throws Throwable {
//                return new ProxyChain(targetClazz,targetObject,targetMethod,methodProxy,params,proxyList).doProxyChain();
//            }
//        });
//    }
    /**
     *
     */
    @SuppressWarnings("unchecked")
    public static <T> T creatProxy(final Class<?> targetClazz, final List<Proxy> proxyList){

        MethodInterceptor methodInterceptor = new MethodInterceptor() {
            @Override
            public Object intercept(Object targetObject, Method targetMethod, Object[] params, MethodProxy methodProxy) throws Throwable {
                ProxyChain proxyChain = new ProxyChain(targetClazz, targetObject, targetMethod, methodProxy, params, proxyList);
                Object obj = proxyChain.doProxyChain();
                return obj;
            }
        };

        Object obj = Enhancer.create(targetClazz, methodInterceptor);
        return (T) obj;
    }
}
